package com.manner.groupon.controller.user;

import com.manner.groupon.common.BaseResponse;
import com.manner.groupon.common.PageRequest;
import com.manner.groupon.common.PageResult;
import com.manner.groupon.common.ResultUtils;
import com.manner.groupon.model.dto.grouponCoupons.BuyGrouponCouponsRequest;
import com.manner.groupon.model.entity.GrouponCoupons;
import com.manner.groupon.model.vo.AdminCouponVO;
import com.manner.groupon.model.vo.GroupCouponVo;
import com.manner.groupon.service.groupon.GrouponCouponsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: Manner
 * @Description: 团购优惠券请求控制器
 * @DateTime: 2024/10/11 20:38
 */
@RestController("userGrouponCouponsController")
@RequestMapping("/groupon-coupons")
@RequiredArgsConstructor

public class GrouponCouponsController {

    private final GrouponCouponsService grouponCouponsService;


    @GetMapping
    public BaseResponse<PageResult<AdminCouponVO>> list(PageRequest page, String storeId, String name, int status) {
        return grouponCouponsService.search(page, storeId, name, status);
    }

    /**
     * 根据编号查询优惠券信息
     *
     * @param id 优惠券编号
     * @return BaseResponse<GroupCouponVo>
     * @author Manner
     * @date 2024/10/13 15:09
     */
    @GetMapping("/{id}")
    public BaseResponse<GroupCouponVo> queryById(@PathVariable Long id) {
        return grouponCouponsService.queryById(id);
    }

    /**
     * 用户下单优惠券
     *
     * @param buyGrouponCouponsRequest 用户下单优惠券请求参数类
     * @return BaseResponse<Integer> 整数响应结果
     * @author Manner
     * @date 2024/10/11 20:54
     */
    @PostMapping("/buy")
    public BaseResponse<String> buyGrouponCoupons(@RequestBody BuyGrouponCouponsRequest buyGrouponCouponsRequest) {
        return grouponCouponsService.addGroupCouponsGenerateOrder(buyGrouponCouponsRequest);
    }


    @PostMapping("/save")
    public BaseResponse<String> save(@RequestBody GrouponCoupons grouponCoupons) {
        grouponCouponsService.save(grouponCoupons);
        //返回数据
        return ResultUtils.success("保存成功");
    }

    @PutMapping
    public BaseResponse<String> update(@RequestBody GrouponCoupons grouponCoupons) {
        grouponCouponsService.updateById(grouponCoupons);
        return ResultUtils.success("修改成功");
    }

    @PutMapping("/{id}/{status}")
    public BaseResponse<String> update(@PathVariable Long id, @PathVariable Integer status) {
        grouponCouponsService.update().set("status", status).eq("id", id).update();
        return ResultUtils.success("修改成功");
    }

    @PutMapping("/cancel/{id}")
    public BaseResponse<String> cancel(@PathVariable Long id) {
        return grouponCouponsService.cancel(id);
    }

    /**
     * @param no        团购订单编号
     * @param longitude 用户所在位置经度
     * @param latitude  用户所在位置纬度
     * @return BaseResponse<String>
     * @description: 用户退款
     * @author Manner
     * @date 2024/12/8 16:01
     */
    @PutMapping("/refund/{no}/{x}/{y}")
    public BaseResponse<String> refund(@PathVariable String no,
                                       @PathVariable("x") Double longitude,
                                       @PathVariable("y") Double latitude) {
        return grouponCouponsService.refund(no, longitude, latitude);
    }
}
